package org.ly.controller;

import org.ly.service.HostMetricService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PrometheusController {

    private final HostMetricService hostMetricService;

    public PrometheusController(HostMetricService hostMetricService) {
        this.hostMetricService = hostMetricService;
    }

    @GetMapping("/exception/metrics")
    public ResponseEntity<String> metrics(){
        String helpHeader = "# HELP north_host_exception_info NorthInterface Exception info.\n";
        String typeHeader = "# TYPE north_host_exception_info gauge\n";
        String metricBody = hostMetricService.exceptionMetric();
        String wholeMetric = "";
        if (metricBody.isEmpty()) {
            wholeMetric = helpHeader + typeHeader;
        } else {
            wholeMetric = helpHeader + typeHeader + metricBody;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_PLAIN);
        return new ResponseEntity<>(wholeMetric, headers, HttpStatus.OK);
    }
}
